package airthmetic.exercise.arr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class _49_字母异位词分组 {
    public static List<List<String>> groupAnagrams(String[] strs) {
        if(strs == null){
            return new ArrayList<List<String>>();
        }

        HashMap<String,List<String>> map = new HashMap<String,List<String>>();
        for(String str : strs){
            String h = hash(str);
            if(!map.containsKey(h)){
                map.put(h,new ArrayList<String>());
            }
            map.get(h).add(str);
        }

        return new ArrayList<List<String>>(map.values());

    }

    public static String hash(String str){
        int[] nums = new int[26];
        for(int i=0; i<str.length(); i++){
            nums[str.charAt(i) - 'a']+=1;
        }

        StringBuilder sb = new StringBuilder();
        for(int i=0; i<nums.length; i++){
            sb.append(nums[i]);
            sb.append('-'); // 这句不能省略  有这种情况  new String[]{"bdddddddddd", "bbbbbbbbbbc"}
        }
        System.out.println(sb.toString());
        return sb.toString();

    }

    public static void main(String[] args) {
        List<List<String>> lists = groupAnagrams(new String[]{"bdddddddddd", "bbbbbbbbbbc"});
        System.out.println(lists);
    }
}
